package wishlistModel;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import productModel.ProductItem;
import spUtil.DB;

public class Wishlist {

	int wishlistId;
	int userId;
	int isPublic;
	String publicLink;
	List<Integer> wishlistItemIds;
	
	public static int getWishlistIdByUserId(int userId){
		int wishlistId = -1;
		ResultSet rs = null;
		String query = "SELECT wishlistId FROM wishlist WHERE userId = " + userId;
//		System.out.println(query);
		try{
			Connection conn = DB.getConnection();
			PreparedStatement st = conn.prepareStatement(query);
			rs = st.executeQuery();
			while(rs.next()){
				wishlistId = rs.getInt("wishlistId");
			}
		}catch (Exception ex) {
			ex.printStackTrace();
		}
		return wishlistId;
	}
	
	public static Wishlist getWishlistByUserId(int userId){
		Wishlist wishlist = new Wishlist();
		ResultSet rs = null;
		String query = "SELECT * FROM wishlist WHERE userId = " + userId;
//		System.out.println(query);
		try{
			Connection conn = DB.getConnection();
			PreparedStatement st = conn.prepareStatement(query);
			rs = st.executeQuery();
			while(rs.next()){
				wishlist.setWishlistId(rs.getInt("wishlistId"));
				wishlist.setUserId(rs.getInt("userId"));
				wishlist.setIsPublic(rs.getInt("isPublic"));
				wishlist.setPublicLink(rs.getString("publicLink"));
			}
		}catch (Exception ex) {
			ex.printStackTrace();
		}
		return wishlist;
	}
	
	public static Wishlist getWishlistByPublicLink(String publicID){
		Wishlist wishlist = new Wishlist();
		ResultSet rs = null;
		String query = "SELECT * FROM wishlist WHERE publicLink = '" + publicID + "'";
//		System.out.println(query);
		try{
			Connection conn = DB.getConnection();
			PreparedStatement st = conn.prepareStatement(query);
			rs = st.executeQuery();
			while(rs.next()){
				wishlist.setWishlistId(rs.getInt("wishlistId"));
				wishlist.setUserId(rs.getInt("userId"));
				wishlist.setIsPublic(rs.getInt("isPublic"));
				wishlist.setPublicLink(rs.getString("publicLink"));
				break;
			}
		}catch (Exception ex) {
			ex.printStackTrace();
		}
		return wishlist;
	}
	
	public static List<Integer> getWishlistItems(int wishlistId){	//to get all the products (productIDs) in one's wishlist
		List<Integer> itemList = new ArrayList<Integer>();
		ResultSet rs = null;
		String query = "SELECT productId FROM wishlistitem wli, wishlist wl WHERE wli.wishlistId = wl.wishlistId AND wl.wishlistId = " + wishlistId + " AND activeStatus = 1";
//		System.out.println(query);
		try{
			Connection conn = DB.getConnection();
			PreparedStatement st = conn.prepareStatement(query);
			rs = st.executeQuery();
			while(rs.next()){
				itemList.add(rs.getInt("productId"));
			}
		}catch (Exception ex) {
			ex.printStackTrace();
		}
		return itemList;
	}
	
	public static ArrayList<ProductItem> sortByPrice(ArrayList<ProductItem> productList, String order){ 	//insertion sort
		int i, j;
		ProductItem key;
		for(j=1; j<productList.size(); j++){
			key = productList.get(j);
			i=j-1;
			if(order.equals("asc")){
				while(i>=0 && (Integer.parseInt(productList.get(i).getPrice()) > Integer.parseInt(key.getPrice()))){
					productList.set(i+1, productList.get(i));
					i = i-1;
				}
			productList.set(i+1, key);
		}
			else if(order.equals("desc")){
				while(i>=0 && (Integer.parseInt(productList.get(i).getPrice()) < Integer.parseInt(key.getPrice()))){
					productList.set(i+1, productList.get(i));
					i = i-1;
				}
			productList.set(i+1, key);
			}
		}
		return productList;
	}
	
	public static void changeWishlistAccess(int wishlistId, int accessSpecifier){
		String query = "UPDATE wishlist SET isPublic = " + accessSpecifier + " WHERE wishlistId = " + wishlistId;
//		System.out.println(query);
		try{
			Connection conn = DB.getConnection();
			PreparedStatement st = conn.prepareStatement(query);
			st.executeUpdate();
		}catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public static boolean isPublicViewAllowed(String publicLink){
		int isPublic = 0;
		ResultSet rs = null;
		String query = "SELECT isPublic FROM wishlist WHERE publicLink = '" + publicLink + "'";
//		System.out.println(query);
		try{
			Connection conn = DB.getConnection();
			PreparedStatement st = conn.prepareStatement(query);
			rs = st.executeQuery();
			while(rs.next()){
				isPublic = rs.getInt("isPublic");
			}
		}catch (Exception ex) {
			ex.printStackTrace();
		}
		if(isPublic == 0)
			return false;
		else
			return true;
	}
	
	public static String getCustomerName(String publicLink){
		String name="";
		ResultSet rs = null;
		String query = "SELECT c.fName, c.lName FROM customer c, wishlist w WHERE c.userId = w.userId AND w.publicLink = '" + publicLink + "'";
//		System.out.println(query);
		try{
			Connection conn = DB.getConnection();
			PreparedStatement st = conn.prepareStatement(query);
			rs = st.executeQuery();
			while(rs.next()){
				name = name.concat(rs.getString("fName"));
				name = name.concat(" ");
				name = name.concat(rs.getString("lName"));
				break;
			}
		}catch (Exception ex) {
			ex.printStackTrace();
		}
		return name;
	}
	
	public static void removeItemFromWishlist(int wishlistId, int productId){
		String query = "UPDATE wishlistitem SET activeStatus = 0" + " WHERE wishlistId = " + wishlistId + " AND productId = " + productId;
//		System.out.println(query);
		try{
			Connection conn = DB.getConnection();
			PreparedStatement st = conn.prepareStatement(query);
			st.executeUpdate();
		}catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public static void addItemToWishlist(int wishlistId, int productId){
		String query = "INSERT INTO wishlistitem (wishlistId, productId) VALUES ('" + wishlistId +"', '"+ productId +"')";
//		System.out.println(query);
		try{
			Connection conn = DB.getConnection();
			PreparedStatement st = conn.prepareStatement(query);
			st.executeUpdate();
		}catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public static void addItemToWishlistByUserId(int userId, int productId){
		int tempWishlistId;
		tempWishlistId = Wishlist.getWishlistIdByUserId(userId);
		addItemToWishlist(tempWishlistId, productId);
	}

	public int getWishlistId() {
		return wishlistId;
	}

	public void setWishlistId(int wishlistId) {
		this.wishlistId = wishlistId;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public int getIsPublic() {
		return isPublic;
	}

	public void setIsPublic(int isPublic) {
		this.isPublic = isPublic;
	}

	public String getPublicLink() {
		return publicLink;
	}

	public void setPublicLink(String publicLink) {
		this.publicLink = publicLink;
	}

	public List<Integer> getWishlistItemIds() {
		return wishlistItemIds;
	}

	public void setWishlistItemIds(List<Integer> wishlistItemIds) {
		this.wishlistItemIds = wishlistItemIds;
	}
}
